//
//  PeopleModel.m
//  ReviewDemo
//
//  Created by 龙 on 2018/3/14.
//  Copyright © 2018年 long. All rights reserved.
//

#import "PeopleModel.h"
#import <objc/runtime.h>
#import "NSObject+NSLog.h"
@implementation PeopleModel

+ (NSDictionary *)arrayContainModelClass
{
    return @{@"carsArray" : @"CarModel",
             @"dogsArray" : @"DogModel"
             };
}

#pragma mark - Public Method

+ (void)getClassInfo
{
    NSLog(@"%s:%@",__FUNCTION__,self);
}

- (void)eatFood:(NSString *)food
{
    NSLog(@"%s  %@",__func__,food);
}

- (NSString *)getHobby
{
    NSLog(@"%s",__func__);
    
    NSLog(@"%s",__FUNCTION__);
    
    return @"sleep";
}

#pragma mark - NSCoding

// 解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self)
    {
        unsigned int count = 0;
        Ivar *ivarList = class_copyIvarList(self.class, &count);
        for (int i = 0; i < count; i ++)
        {
            Ivar ivar = ivarList[i];
            NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
            [self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
        }
        free(ivarList);
    }
    return self;
}

//归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    unsigned int count = 0;
    Ivar *ivarList = class_copyIvarList(self.class, &count);
    //但凡在C语言里面 看到New Creat Copy 都需要释放
    for (int i = 0; i < count; i ++)
    {
        Ivar ivar = ivarList[i];
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        [aCoder encodeObject:[self valueForKey:key] forKey:key];
    }
    free(ivarList);
}

- (BOOL)isEqual:(id)object
{
    if (self == object)//同一个实例
    {
        return YES;
    }
    
    if ([object isKindOfClass:[self class]] == NO)//不同一个对象
    {
        return NO;
    }
    
    BOOL isSame = YES;
    unsigned int count = 0;
    Ivar *ivarList = class_copyIvarList(self.class, &count);
    for (int i = 0; i < count; i ++)
    {
        Ivar ivar = ivarList[i];
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        id oldValue = [self valueForKey:key];
        id newValue = [object valueForKey:key];
        if (oldValue == nil && newValue == nil)
        {
            
        }
        else
        {
            if ([oldValue isEqual:newValue] == NO)
            {
                isSame = NO;
                break;
            }
        }
    }
    free(ivarList);
    
    return isSame;
}


@end
